define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                rowStyle: function (row, index) {
                    console.log(row.order_switch);
                    if (row.order_switch === 1) {
                        return {
                            css: {"color" : 'red !important'} // Bootstrap 浅色成功类
                        };
                    } else {
                        return {};
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        //{field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        //{field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        //{field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'invitation', title: '邀请码', operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'commission', title: '福利券', operate: 'BETWEEN', sortable: true},
                        {field: 'gold_bean', title: '金豆', operate: 'BETWEEN', sortable: true},
                        {field: 'shopping_score', title: '购物积分', operate: 'BETWEEN', sortable: true},
                        {field: 'money', title: '消费券', operate: 'BETWEEN', sortable: true},
                        {field: 'tokens', title: '通证', operate: 'BETWEEN', sortable: true},
                        {field: 'total_score', title: '总贡献值', operate: 'BETWEEN', sortable: true},
                        {field: 'score', title: '剩余贡献值', operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'moy_switch', title: '消费券开关', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'wit_switch', title: '福利券开关', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'bean_switch', title: '金豆开关', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'is_admin', title: '监管开关', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'wait_lock_status', title: '待锁定状态', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'order_switch', title: '空单开关', searchList: {"0":'关闭',"1":'开启'}, formatter: Table.api.formatter.toggle},
                        {field: 'level_down', title: '是否降级', searchList: {"0":'是',"1":'否'}, formatter: Table.api.formatter.toggle},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        //{field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        //{field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        //{field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'lock_status', title: '锁状态', searchList: {"0":'正常',"1":'不能提现交易', '2': '不能登录'}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons: [
                                {
                                    name: 'funds',
                                    text: '充值/扣费',
                                    title: '充值/扣费',
                                    classname: 'btn  btn-success btn-dialog',
                                    url: 'user/user/funds',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                // {
                                //     name: 'newscore',
                                //     text: '重置总贡献值',
                                //     title: '重置总贡献值',
                                //     classname: 'btn  btn-info btn-ajax',
                                //
                                //     confirm: '确认重置吗?',
                                //     url: 'user/user/newscore?ids={row.id}',
                                //     success: function (data, ret) {
                                //         $(".btn-refresh").trigger("click");
                                //     },
                                //     error: function (data, ret) {
                                //         $(".btn-refresh").trigger("click");
                                //     },
                                //     callback: function (data) {
                                //         $(".btn-refresh").trigger("click");
                                //     }
                                // },

                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        funds: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});